package com.itheima.demo6;

import java.util.concurrent.Semaphore;

public class Demo {
    public static void main(String[] args)  {

        for (int i = 0; i < 100; i++) {
            new SemaphoreTest().start();
        }



    }
}

class SemaphoreTest extends Thread{
    static Semaphore semaphore = new Semaphore(2);

    @Override
    public void run() {

            try {
                semaphore.acquire();
                System.out.println("获得了通行证开始行驶");


            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("归还通行证");
                semaphore.release();

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
